package formServlets;
import java.io.IOException;
import java.io.PrintWriter;

import javax.jdo.PersistenceManager;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.DatastoreAttributes;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;

import javax.servlet.http.HttpServlet;

//import guestbook.PMF;

public class  AddSchoolInfoServlet  extends HttpServlet  {

	/////////////////////////////////////////////////////////////////////
	// doGet forwards the request to doPost, the code does the same thing
	// whether the post method is GET or POST
	////////////////////////////////////////////////////////////////////
	
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		getServletContext().getRequestDispatcher("SNSP.html").forward(req, resp);
	}
 
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		////////////////////////////////////////////////////////////////////
		// Get all the cookies from the webpage that called this servlet
		// Check for a cookie called passUsername
		// record that cookie
		////////////////////////////////////////////////////////////////////
		
	/*	Cookie cookies [] = request.getCookies ();
		Cookie myCookie = null;
		if (cookies != null)
		{
			for (int i = 0; i < cookies.length; i++)
			{
				if (cookies [i].getName().equals ("passUsername"))
				{
				myCookie = cookies[i];
				break;
				}
			}
		}
		*/
//		PrintWriter pw = response.getWriter(); 
		
		// Open access to the database
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		
		// Fetch the information from the form submitted by the page that called this servlet
		
		System.out.println("in schoolinfoservlet");
		String countrySelect= request.getParameter("countrySelect");
		String CountryInput=request.getParameter("countryInputBox");
		String stateSelect= request.getParameter("stateSelect");
		String StateInput=request.getParameter("stateInputBox");
		String schoolSelect= request.getParameter("schoolSelect");
		String schoolInput=request.getParameter("schoolInputbox");
		System.out.println("schoolselect: "+schoolSelect+" schoolinput: "+schoolInput);
		String website= request.getParameter("website");
		String placeId=new String();
		String schoolId=new String();
		if(countrySelect.equalsIgnoreCase("country"))
		{
			
			System.out.println("in country");
			placeId=CountryInput+StateInput;
			Entity user = new Entity("PlaceInfo",placeId);
			user.setProperty("placeId", placeId);
			user.setProperty("Country",CountryInput);
			user.setProperty("State", StateInput);
			datastore.put(user);
			schoolId=placeId+schoolInput;
			user = new Entity("SchoolInfo",schoolId);
			user.setProperty("placeId", placeId);
			user.setProperty("schoolId", schoolId);
			user.setProperty("schoolName", schoolInput);
			user.setProperty("website", website);
			user.setProperty("totalStudents", 1);
			user.setProperty("avgGREScore", 0);
			user.setProperty("employmentPercentage", 0.0);
			user.setProperty("avgStudentGPA", 0.0);
			user.setProperty("percerntOfFundedStudents", 0.0);
			datastore.put(user);
			 
		}
		else
		if(stateSelect.equalsIgnoreCase("States"))
		{
			
			System.out.println("in state");
		    placeId=countrySelect+StateInput;
			Entity user = new Entity("PlaceInfo",placeId);
			user.setProperty("placeId", placeId);
			user.setProperty("Country",countrySelect);
			user.setProperty("State", StateInput);
			datastore.put(user);
			schoolId=placeId+schoolInput;
			user = new Entity("SchoolInfo",schoolId);
			user.setProperty("placeId", placeId);
			user.setProperty("schoolId", schoolId);
			user.setProperty("schoolName", schoolInput);
			user.setProperty("website", website);
			user.setProperty("totalStudents", 1);
			user.setProperty("avgGREScore", 0);
			user.setProperty("employmentPercentage", 0.0);
			user.setProperty("avgStudentGPA", 0.0);
			user.setProperty("percerntOfFundedStudents", 0.0);
			datastore.put(user);
		}
		else
		if(schoolSelect.equalsIgnoreCase("School"))
		{
			System.out.println("in school");
			Query q1 = new Query("PlaceInfo");
			q1.addFilter("Country", Query.FilterOperator.EQUAL,countrySelect  );
			q1.addFilter("State", Query.FilterOperator.EQUAL,stateSelect  );
			PreparedQuery pq1 = datastore.prepare(q1);
			for (Entity result : pq1.asIterable()) {
				 placeId = (String) result.getProperty("placeId");
			System.out.println("placeid= "+placeId);
			}
			Entity user = new Entity("SchoolInfo",placeId);
			schoolId=placeId+schoolInput;
			user = new Entity("SchoolInfo",schoolId);
			user.setProperty("placeId", placeId);
			user.setProperty("schoolId", schoolId);
			user.setProperty("schoolName", schoolInput);
			user.setProperty("website", website);
			user.setProperty("totalStudents", 1);
			user.setProperty("avgGREScore", 0);
			user.setProperty("employmentPercentage", 0.0);
			user.setProperty("avgStudentGPA", 0.0);
			user.setProperty("percerntOfFundedStudents", 0.0);
			datastore.put(user);
			
		}
		else
		{
			System.out.println("in else");
			placeId=countrySelect+stateSelect;
			schoolId=countrySelect+stateSelect+schoolSelect;
			Query q1 = new Query("SchoolInfo");
			q1.addFilter("schoolId", Query.FilterOperator.EQUAL,schoolId  );
			PreparedQuery pq1 = datastore.prepare(q1);
			int count=pq1.countEntities(FetchOptions.Builder.withPrefetchSize(1));
			System.out.println("number of entries in database with same placeID= "+ count);
			
			for (Entity result : pq1.asIterable())
			{
				String schoolname= (String) result.getProperty("schoolName");
				System.out.println("school name: "+schoolname);
			//	System.out.println(result.getProperty("totalStudents"));
				Long totalStudents= (Long) result.getProperty("totalStudents");
		     	System.out.println("totalStudents: "+ totalStudents);
				result.setProperty("totalStudents", totalStudents+1);
		
			//	result.setProperty("totalStudents", (Integer)result.getProperty("totalStudents")+1);
			}
		}
		System.out.println("placeid: "+ placeId+" schoolid: "+schoolId);
		Cookie placeCookie = new Cookie("placeId", placeId);
		Cookie schoolCookie = new Cookie("schoolId", schoolId);
		
		Query q1 = new Query("SchoolInfo");
		PreparedQuery pq1 = datastore.prepare(q1);
		for (Entity result : pq1.asIterable()) 
		{
		String	schoolId1 = (String) result.getProperty("schoolId");
			System.out.println("schoolid: "+schoolId1);
			Long totalStudents=(Long) result.getProperty("totalStudents");
			System.out.println("totalStudents: "+totalStudents);
		}
		
	    response.addCookie(placeCookie);
	    response.addCookie(schoolCookie);

	    System.out.println("In AddSchoolInfoServlet");
	    Cookie cookies [] = request.getCookies ();
	    for (int i = 0; i < cookies.length; i++){
	    	System.out.println("cookie " + i + " is " + cookies[i].getName() + " value = "+ cookies[i].getValue());
	    }
		  
	}
 
}
